import Vue from 'vue'
import axios from '../modules/ajax-mid/index'
let isLeader = null
function getIsLeader () {
  let {loading, toast} = Vue
  return new Promise(function (resolve, reject) {
    if (isLeader === null) {
      loading.show()
      axios.get('/lecture/v1/isLeader').then(({data}) => {
        if (data.status === 200) {
          isLeader = data.result.is_leader
          resolve(isLeader)
        } else {
          return Promise.reject(new Error(data.message))
        }
      }).catch(e => {
        toast.error(e.message)
        reject(e)
      }).then(() => {
        loading.hide()
      })
    } else {
      resolve(isLeader)
    }
  })
}

getIsLeader.set = function () {
  isLeader = null
}

export default getIsLeader
